package com.eric.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.eric.domain.dto.WarehouseLayoutDTO;
import com.eric.service.WarehouseLayoutService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/warehouseLayout")
@RequiredArgsConstructor
public class WarehouseLayoutController {
    private final WarehouseLayoutService warehouseLayoutService;

    @GetMapping("/getAllByPage")
    public SaResult getAllByPage(Integer current, Integer pageSize){
        return warehouseLayoutService.getAllByPageService(current,pageSize);
    }

    @GetMapping("getAllCounts")
    public SaResult getAllCounts(){
        return warehouseLayoutService.getAllCountsService();
    }



    @PostMapping("saveOne")
    public SaResult saveOne(@RequestBody WarehouseLayoutDTO warehouseLayoutDTO){
        return warehouseLayoutService.saveOneService(warehouseLayoutDTO);
    }

    @GetMapping("getById/{id}")
    public SaResult getById(@PathVariable Integer id){
        return warehouseLayoutService.getByIdService(id);
    }

    @GetMapping("getFirstLevel")
    public SaResult getFirstLevel(){
        return warehouseLayoutService.getFirstLevelService();
    }

    @GetMapping("getSecondLevelByPid")
    public SaResult getSecondLevelByPid(Integer pid){
        if (pid == null || pid <0) {
            return SaResult.error("id有误");
        }
        return warehouseLayoutService.getSecondLevelByPidService(pid);
    }

    @DeleteMapping("deleteById/{id}")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteById(@PathVariable Integer id){
        if (id == null || id <1) {
            return SaResult.error("id有误");        }
        return warehouseLayoutService.deleteByIdService(id);
    }

    @PutMapping("update")
    @SaCheckPermission("sys:update")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult updateById(@RequestBody WarehouseLayoutDTO warehouseLayoutDTO){

        if (BeanUtil.isEmpty(warehouseLayoutDTO) || warehouseLayoutDTO.getId() == null || warehouseLayoutDTO.getId() <1) {
            return SaResult.error("对象错误");
        }
        return warehouseLayoutService.updateByIdService(warehouseLayoutDTO);
    }

    @GetMapping("getSecondLevel")
    public SaResult getSecondLevel(){
        return warehouseLayoutService.getSecondLevelService();
    }

    @GetMapping("getThirdLevel")
    public SaResult getThirdLevel(){
        return warehouseLayoutService.getThirdLevelService();
    }

    @GetMapping("getOneByPid")
    public SaResult getOneByPid(Integer pid){
        if (pid == null || pid <0) {
            return SaResult.error("id有误");
        }
        return warehouseLayoutService.getOneByPidService(pid);
    }

    @GetMapping("getByContainerName")
    public SaResult getByContainerName(String containerName){
        if (containerName == null || containerName.isEmpty()) {
            return SaResult.error("容器名称有误");
        }
        return warehouseLayoutService.getByContainerNameService(containerName);
    }


    @DeleteMapping("deleteByContainerName")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteByContainerName(String containerName){
        if (containerName == null || containerName.isEmpty()) {
            return SaResult.error("容器名称有误");
        }
        return warehouseLayoutService.deleteByContainerNameService(containerName);
    }

}